project (SagittariusSrc)

set(SOURCES 
    builtins/builtins.c
    builtins/builtins.cpp
    builtins/builtinfun.c
    builtins/modules/builtinmod.c
    builtins/modules/io.c
    builtins/modules/io.cpp
    builtins/modules/sbml.c
    builtins/modules/sbml.cpp
    compiler/compiler.c
    core/SagittariusException.cpp
    ${SAGITTARIUS_GREYLIST_IMPLC}
    hash/hash.cpp
    hash/MurmurHash3.cpp
    io/io_generic.c
    lexer/dfa.c
    lexer/lAST.c
    lexer/lDeriv.c
    lexer/lexcore.c
    lexer/lexcore.cpp
    lexer/lexsymtab.c
    lexer/lexvec.c
    lexer/lexx.c
    lexer/lParser.c
#     lexer/plan3.c
#     lexer/plan4.c
    lexer/plan5.c
    lexer/sm.c
    lexer/sm_c.c
    lexer/treewalk.c
    mem/dumballoc.c
    mem/lsegalloc.c
    mem/pageheap.c
    mem/sizeclass.c
    mem/span.c
    mem/span_smo.c
    mod/modloader.c
    mod/module.c
    #parallel/clone.c
    parallel/multisema.c
    parallel/thread.c
    parser/parser.c
    parser/pAST.c
    parser/scanner.c
    patterns/hashtable_th.c
    patterns/hashtable_nth.c
    patterns/radixtree.c
    #parallel/wrfutex.c
    #parallel/pfor.c
    re/reCS.c
    re/reDC.c
    #re/reAST.c
    #re/reParser.c
    session/session.c
    symtab/set_uint64.c
    symtab/symtab.c
    #types/typetable.c
    unicode/CUnicode.c
    util/sg_alphanum.c
    )
    
if(LINK_WITH_LLVM)
    set(SOURCES ${SOURCES} 
        backend/llvm/backend.cpp
        backend/llvm/codegen.cpp
        backend/llvm/llvmsupport.cpp
        backend/llvm/llvm_types.cpp
        backend/llvm/support.cpp)
endif()

set(HEADERS 
    builtins/builtins.h
    builtins/builtinfun.h
    builtins/modules/builtinmod.h
    builtins/modules/io.h
    builtins/modules/sbml.h
    compiler/compiler.h
    core/SagittariusException.hpp
    ${SAGITTARIUS_GREYLIST_IMPLC}
    lexer/dfa.h
    lexer/lAST.h
    lexer/lDeriv.h
    lexer/lexcore.h
    lexer/lexsymtab.h
    lexer/lexvecb.h
    lexer/lexx.h
    lexer/lParser.h
    lexer/sm.h
    lexer/sm_c.h
    lexer/treewalk.h
    mem/dumballoc.h
    mem/lsegalloc.h
    mem/pageheap.h
    mem/sizeclass.h
    mem/span.h
    mem/span_smo.h
    mod/modloader.h
    mod/module.h
    parallel/multisema.h
    parallel/thread.h
    parser/parser.h
    parser/pAST.h
    parser/scanner.h
    patterns/hashtable_th.h
    patterns/hashtable_nth.h
    patterns/radixtree.h
    re/reCS.h
    re/reDC.h
    session/session.h
    symtab/set_uint64.h
    symtab/symtab.h
    #types/typetable.h
    types/types.h
    unicode/CUnicode.h
    util/sg_alphanum.h
    )

if(LINK_WITH_GMP)
    set(GMP_SOURCES
        gmp/gmp_set.c
        gmp/gmpsupport.c
        unicode/GMPUnicode.c
        )
    set(GMP_HEADERS
        gmp/gmp_set.h
        gmp/gmpsupport.h
        unicode/GMPUnicode.h
        )
endif()

if(${UNIX})
    set(ASM_SOURCES
        parallel/atomics_amd64
        parallel/thread_linux_amd64
        )
elseif(${WIN32})
    set(ASM_SOURCES
        parallel/atomics_amd64
        )
endif()
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/parallel) #I hate CMake

#ASM_OBJECTS
foreach(ASMSRC ${ASM_SOURCES})
    set(ASM_OBJECTS ${ASM_OBJECTS};${CMAKE_CURRENT_BINARY_DIR}/${ASMSRC}.o)
endforeach()
message("ASM_OBJECTS = ${ASM_OBJECTS}")

if(${WIN32})
    set(PLATFORM_SOURCES
        io/io_win.c
        mem/sysalloc_win.c)
else()
    set(PLATFORM_SOURCES
        io/io_lin.c
        mem/sysalloc_lin.c
        parallel/mutex_lin.c)
    if(USING_CLANG)
        set_source_files_properties(mem/sysalloc_lin.c PROPERTIES COMPILE_FLAGS "-std=gnu89")
    endif()
endif()

configure_file(core/config.h.in core/config.h)

#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -E")
set(SAGITTARIUS_SOURCES ${SOURCES} ${GMP_SOURCES} ${PLATFORM_SOURCES})
add_library(sagittarius ${SAGITTARIUS_SOURCES})
if(LINK_WITH_LLVM)
    target_link_libraries(sagittarius ${LLVM_LIBRARIES} ${LLVMAuxLibraries})
endif()

#if(${UNIX})
    if(NOT ${MINGW})
        add_library(asmlib STATIC ${ASM_OBJECTS})
        set_source_files_properties(${ASM_OBJECTS} PROPERTIES EXTERNAL_OBJECT true GENERATED true)
        set_target_properties(asmlib PROPERTIES LINKER_LANGUAGE C ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) #OBJECT_DEPENDS ${ASM_OBJECTS})
        foreach(ASMSRC ${ASM_SOURCES})
            add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ASMSRC}.o COMMAND ${SG_ASSEMBLER} ${SG_ASM_FORMAT} -o ${CMAKE_CURRENT_BINARY_DIR}/${ASMSRC}.o ${CMAKE_CURRENT_SOURCE_DIR}/${ASMSRC}.s DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${ASMSRC}.s)
        endforeach()
        target_link_libraries(sagittarius asmlib)
    endif()
#endif()

if(LINK_WITH_GMP)
    target_link_libraries(sagittarius ${GMPLibs})
endif()

if(ENABLE_LIBSBML)
    target_link_libraries(sagittarius ${libSbmlLibs})
endif()

#Doxygen
#Other files besides headers that contain documentation
set(DOXYGEN_OTHER_FILES 
    lexer/sm_c.c
    )
set(SAGITTARIUS_DOXY_INPUT "")
foreach(f ${HEADERS} ${GMP_HEADERS} ${DOXYGEN_OTHER_FILES})
    set(SAGITTARIUS_DOXY_INPUT "${SAGITTARIUS_DOXY_INPUT} ${CMAKE_CURRENT_SOURCE_DIR}/${f}")
endforeach()
configure_file(../doxy/doxy.cfg ../doxy/doxy.cfg)
add_custom_target(docs COMMAND doxygen ../doxy/doxy.cfg)
